package com.luxoft.scoremanager.testpack;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

import static com.luxoft.scoremanager.testpack.BrowserType.*;

public class SwingHelper extends JDialog {

    private static java.util.List<BrowserType> drivers;

    static volatile boolean submitState = false;

    Toolkit toolkit;
    Dimension screenSize;
    JCheckBox checkFirefox;
    JCheckBox checkChrome;
    JCheckBox checkIE;
    JButton submit;

    private SwingHelper() {

        toolkit = Toolkit.getDefaultToolkit();
        screenSize = toolkit.getScreenSize();
        checkFirefox = new JCheckBox("Firefox");
        checkChrome = new JCheckBox("Chrome");
        checkIE = new JCheckBox("Internet Explorer");
        submit = new JButton("Submit");


        setTitle("Choose a driver");
        setSize(200, 125);
        setResizable(false);
        setLocation((int) ((screenSize.getWidth() - getWidth()) / 2), (int) ((screenSize.getHeight() - getHeight()) / 2));
        setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        add(checkFirefox);
        add(checkChrome);
        add(checkIE);
        add(submit);
        setVisible(true);
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                drivers = new ArrayList();
                if (checkFirefox.isSelected())
                    drivers.add(FIREFOX);
                if (checkChrome.isSelected())
                    drivers.add(CHROME);
                if (checkIE.isSelected())
                    drivers.add(INTERNET_EXPLORER);
                dispose();
                submitState = true;
            }
        });
    }

    public static java.util.List<BrowserType> getDrivers() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SwingHelper();
            }
        });
        while (!submitState) {
        }

        return drivers;
    }
}
